//
//  DynamicSendViewController.m
//  MJApp
//
//  Created by wangliang on 15/5/27.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "DynamicSendViewController.h"
#import "UITextView+WLCategory.h"
#import "PhotoView.h"
#import "WLJSON.h"
#import "TopicViewController.h"

@interface DynamicSendViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) NSMutableArray *imageS;
@property (nonatomic, assign) NSInteger currIndex;
@property (nonatomic,strong)UITextView *textView;

@end

@implementation DynamicSendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self UI];
    [self NaviUI];
    
}

- (NSMutableArray *)imageS {
    
    if (!_imageS) {
        
        _imageS = [NSMutableArray array];
    }
    return _imageS;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)UI {
    
    self.title = @"发表图文";
    self.view.backgroundColor = Color_Back_Gray;
    
    CGFloat wide = CGRectGetWidth(self.view.frame) - 20;
    CGFloat high = wide * 200.0f / 325.0f;
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, wide, high)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    UIImage *image = [UIImage imageNamed:@"hd_19"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, image.size.width, image.size.height)];
    imageView.image = image;
    [backView addSubview:imageView];

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame), 0, CGRectGetWidth(backView.frame) - CGRectGetMaxX(imageView.frame), CGRectGetHeight(backView.frame))];
    [backView addSubview:textView];
    [textView becomeFirstResponder];
    UILabel *phLabel = [[UILabel alloc] init];
    phLabel.text = @"发表图文...";
    phLabel.textColor = [UIColor grayColor];
    textView.font = [UIFont systemFontOfSize:15];
    [textView addPlaceholder:phLabel];
    self.textView = textView;
    
    UIButton *activeButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(backView.frame) - 40, CGRectGetHeight(backView.frame) - 40, 40, 40)];
    [activeButton setTitle:@"＃" forState:UIControlStateNormal];
    [activeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    activeButton.titleLabel.font = [UIFont boldSystemFontOfSize:25];
    [backView addSubview:activeButton];
    [activeButton addTarget:self action:@selector(activeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    PhotoView *photoView = [[PhotoView alloc] initWithFrame:CGRectMake(CGRectGetMinX(backView.frame), CGRectGetMaxY(backView.frame), CGRectGetWidth(backView.frame), CGRectGetHeight(self.view.frame) - CGRectGetMaxY(backView.frame) - 50)];
    photoView.backgroundColor = Color_Back_Gray;
    photoView.itemSize = CGSizeMake(90, 90);
    photoView.delegate = self;
    [photoView photoViewCheckPhoto:^(NSDictionary *infoDic) {
        
        self.currIndex = [infoDic[@"index"] integerValue];
        [self addImage:infoDic[@"image"]];
        
    } whitDeletePhoto:^(NSDictionary *infoDic) {
        
        self.currIndex = [infoDic[@"index"] integerValue];
        [self deleteImage];
    }];
    [self.view addSubview:photoView];
    
    
}

- (void)NaviUI {
    
    [self.rightBtn setTitle:@"发送" forState:UIControlStateNormal];
}

- (void)activeButtonAction:(UIButton *)sender {
    
    [self.navigationController pushViewController:[[TopicViewController alloc] init] animated:YES];
}

- (void)addImage:(UIImage *)image {
    
    NSDictionary *dic = @{
                          @"filename":image,
                          @"type":@"trend"
                          };
    [WLHttpRequestAfter userUploadImageWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            if (self.currIndex < self.imageS.count) {
                
                [self.imageS replaceObjectAtIndex:self.currIndex withObject:resultDictionary[@"data"][@"url"]];
            }else {
                
                [self.imageS addObject:resultDictionary[@"data"][@"url"]];
            }
        }
    } andFailureBlock:^(NSError *resultError) {
        
    }];

}

- (void)deleteImage {
    
    [self.imageS removeObjectAtIndex:self.currIndex];

}

- (void)clickRightButton {
    
    
    NSString *contentStr = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([contentStr isEqual:@""] && self.imageS.count == 0) {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"输入文字不能为空！"];
    }else {
        
        NSDictionary *dic = nil;
        
        if (self.imageS.count > 0) {
            
            dic = @{
                    @"user_id":[userInfoTool share].ID,
                    @"trends":self.textView.text,
                    @"trends_address":@"ssssssss",
                    @"images":[WLJSON arrayOneToJson:self.imageS]
                    };
        }else {
            
            dic = @{
                    @"user_id":[userInfoTool share].ID,
                    @"trends":self.textView.text,
                    @"trends_address":@"ssssssss"
                    };
        }
        
        
        [WLHttpRequestAfter userPublishTrendWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
            
            if ([resultDictionary[@"status"] integerValue] == 1) {
                
                [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"发布成功"];
                [self.imageS removeAllObjects];
                [self.navigationController popViewControllerAnimated:YES];

            }else {
                
                [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
            }
            
        } andFailureBlock:^(NSError *resultError) {
            
        }];
    }
}

-  (void)dealloc {
    
    NSLog(@"dealloc");
    [self.textView removeNotice];
}

@end
